package com.liveneo.adapter.mobile.web.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.liveneo.adapter.base.entity.PrpmCamponqueueLog;
import com.liveneo.adapter.base.entity.PrpmCaseInfo;
import com.liveneo.adapter.base.entity.PrpmCaseInfoCyp;
import com.liveneo.adapter.base.entity.PrpmDictDetail;
import com.liveneo.adapter.base.entity.PrpmLossItem;
import com.liveneo.adapter.base.entity.PrpmLossItemCheckCyp;
import com.liveneo.adapter.base.entity.PrpmLossItemCyp;
import com.liveneo.adapter.base.entity.PrpmLossItemCypHistory;
import com.liveneo.adapter.base.entity.PrpmSurveyUser;
import com.liveneo.adapter.base.entity.PrpmUploadLossItemImg;
import com.liveneo.adapter.base.entity.ReassignMent;
import com.liveneo.adapter.base.support.AdapterConstants;
import com.liveneo.adapter.base.support.BaseController;
import com.liveneo.adapter.base.support.LogUtils;
import com.liveneo.adapter.mobile.model.BaseResponse;
import com.liveneo.adapter.mobile.model.dto.CaseInfoCDto;
import com.liveneo.adapter.mobile.model.dto.CaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.CaseInfoDtoPn;
import com.liveneo.adapter.mobile.model.dto.LossItemDto;
import com.liveneo.adapter.mobile.model.dto.MobileQueueDto;
import com.liveneo.adapter.mobile.model.dto.PolicyCaseInfoDto;
import com.liveneo.adapter.mobile.model.dto.PolicyItemDto;
import com.liveneo.adapter.mobile.model.dto.PrpmLossitemcheck78Dto;
import com.liveneo.adapter.mobile.model.dto.ReviewImage;
import com.liveneo.adapter.mobile.model.request.AffirmClaimCaseRequest;
import com.liveneo.adapter.mobile.model.request.CaseInfoDtoRequest;
import com.liveneo.adapter.mobile.model.request.CaseInfoSelfRequest;
import com.liveneo.adapter.mobile.model.request.CaseStatusRequest;
import com.liveneo.adapter.mobile.model.request.DamageListRequest;
import com.liveneo.adapter.mobile.model.request.GetPolicyItem31Request;
import com.liveneo.adapter.mobile.model.request.PolicyInfoRequest;
import com.liveneo.adapter.mobile.model.request.SurveyUserCodeRequest;
import com.liveneo.adapter.mobile.model.request.TurnPersonSurveyRequest;
import com.liveneo.adapter.mobile.model.response.CaseInfoDtoPnResponse;
import com.liveneo.adapter.mobile.model.response.CaseInfoDtoResponse;
import com.liveneo.adapter.mobile.model.response.CaseInfoSelfResponse;
import com.liveneo.adapter.mobile.model.response.CaseStatusResponse;
import com.liveneo.adapter.mobile.model.response.DamageListResponse;
import com.liveneo.adapter.mobile.model.response.GetPolicyItem31Response;
import com.liveneo.adapter.mobile.model.response.KpiInfoResponse;
import com.liveneo.adapter.mobile.model.response.PolicyInfoResponse;
import com.liveneo.adapter.mobile.service.LossItemService;
import com.liveneo.adapter.mobile.service.MobileCaseService;
import com.liveneo.adapter.mobile.service.PrpmCamponqueueLogService;
import com.liveneo.adapter.mobile.service.PrpmCaseInfoCypService;
import com.liveneo.adapter.mobile.service.PrpmDictDetailService;
import com.liveneo.adapter.mobile.service.PrpmLossItemCheckCypService;
import com.liveneo.adapter.mobile.service.PrpmLossItemCypHistoryService;
import com.liveneo.adapter.mobile.service.PrpmLossItemCypService;
import com.liveneo.adapter.mobile.service.PrpmSurveyUserService;
import com.liveneo.adapter.mobile.service.PrpmUploadLossItemImgService;
import com.liveneo.adapter.mobile.service.bussniess.MobileBussService;
import com.liveneo.adapter.mobile.support.MobileReturnCodeSupport;
import com.liveneo.adapter.mobile.util.Constant;
import com.liveneo.adapter.mobile.util.DateUtil;
import com.liveneo.adapter.mobile.util.UUIDUtils;
import com.liveneo.adapter.outinterface.util.ResultInfo;
import com.liveneo.adapter.outinterface.web.platform.ClaimInterface;
import com.liveneo.system.base.utils.DateUtils;
import com.liveneo.system.base.utils.EncryptUtil;
import com.liveneo.system.base.utils.JsonUtils;
import com.liveneo.system.base.utils.OssManage;

/**
 * 移动端案件相关接口
 * 
 * @author wangxx
 * 
 * @since 2016-06-12
 */
@Controller
@RequestMapping("/mobile")
public class MobileCaseController extends BaseController {
    @Autowired
    private MobileCaseService            mobileCaseService;
    @Autowired
    private LossItemService              lossItemService;
    @Autowired
    private PrpmLossItemCypService       lossItemCypService;
    @Autowired
    private PrpmUploadLossItemImgService prpmUploadLossItemimgService;
    @Autowired
    private PrpmSurveyUserService        prpmSurveyUserService;
    @Autowired
    private MobileBussService            mobileBussService;
    @Autowired
    private PrpmCaseInfoCypService       caseInfoCypService;
    @Autowired
    private PrpmLossItemCheckCypService  lossItemCheckCypService;
    @Autowired
    private PrpmCamponqueueLogService    prpmCamponqueueLogService;
    @Autowired
    private PrpmLossItemCypHistoryService lossItemCypHistoryService;
    @Autowired
    private PrpmDictDetailService prpmDictDetailService;
    
    private static String DT_CODE = "DICT_TYPE_REASSIGN_ACQUIR";

    /**
     * @Title: 查询保单信息 @Description:  @param @param data @param @return
     * 设定文件 @return String 返回类型 @throws
     */
    @RequestMapping("/getPolicyInfo.action")
    @ResponseBody
    public String getPolicyInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "getPolicyInfo.action入参=" + logUUid;
        String result = null;
        PolicyInfoResponse response = null;
        try {
            PolicyInfoRequest pr = JsonUtils.fromJsonWithBase64(logTitle, data, PolicyInfoRequest.class);
            PrpmCaseInfo caseInfo = mobileCaseService.getCaseInfoByCaseNo(pr.getCaseNo());
            if (caseInfo != null) {
                PolicyCaseInfoDto pcaseInfo = new PolicyCaseInfoDto();
                pcaseInfo.setCaseNo(caseInfo.getCaseNo());
                pcaseInfo.setCaseStatus(caseInfo.getCaseStatus());
                pcaseInfo.setContact(caseInfo.getContact());
                pcaseInfo.setContactPhone(caseInfo.getContactPhone());
                if (caseInfo.getDamageDate() != null) {
                    pcaseInfo.setDamageDate(DateUtils.formatDateTime(caseInfo.getDamageDate()));
                }
                pcaseInfo.setDamagePlace(caseInfo.getDamagePlace());
                pcaseInfo.setDelegateReamrk(caseInfo.getDelegateReamrk());
                pcaseInfo.setLicenceNo(caseInfo.getLicenseNo());
                pcaseInfo.setSurveyPalce(caseInfo.getSurveyPlace());
                pcaseInfo.setUserName(caseInfo.getUserName());
                pcaseInfo.setUuid(caseInfo.getCaseInfoId());
                List<PolicyItemDto> itemList = new ArrayList<PolicyItemDto>();
                // 待补全调用核心
                itemList.addAll(createItemList());
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        PolicyInfoResponse.class);
                response.setCaseInfo(pcaseInfo);
                response.setPolicyItemDtoList(itemList);
            } else {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_CASENO_NO_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                        PolicyInfoResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("查询保单信息系统异常，错误信息:", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    PolicyInfoResponse.class);
        }
        // 响应结果
        result = JsonUtils.toJson(response);
        LogUtils.info("查询保单信息，响应结果：" + logUUid + result);
        result = EncryptUtil.encodeBase64(result);
        return result;
    }

    private List<PolicyItemDto> createItemList() {
        List<PolicyItemDto> itemList = new ArrayList<PolicyItemDto>();
        PolicyItemDto item = new PolicyItemDto();
        item.setInsurantName("张三");
        item.setTbName("李四");
        item.setPolicyNo("20160628001");
        item.setRiskCode("code1");
        item.setRiskName("车险1");
        item.setRiskClass("车险");
        itemList.add(item);
        PolicyItemDto item1 = new PolicyItemDto();
        item1.setInsurantName("张三");
        item1.setTbName("李四");
        item1.setPolicyNo("20160628002");
        item1.setRiskCode("code2");
        item1.setRiskName("车险2");
        item1.setRiskClass("车险");
        itemList.add(item1);
        PolicyItemDto item2 = new PolicyItemDto();
        item2.setInsurantName("张三");
        item2.setTbName("李四");
        item2.setPolicyNo("20160628003");
        item2.setRiskCode("code3");
        item2.setRiskName("车险3");
        item2.setRiskClass("车险");
        itemList.add(item2);
        return itemList;
    }

    /**
     * 绩效看板信息
     */
    @RequestMapping("getKpiInfo.action")
    @ResponseBody
    public String getKpiInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "getKpiInfo.action入参=" + logUUid;
        SurveyUserCodeRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, SurveyUserCodeRequest.class);
        KpiInfoResponse response = null;
        String result = "";
        try {
            String surveyUserCode = request.getUserCode();
            // 实现获取绩效看板
            LogUtils.debug("surveyUserCode:" + surveyUserCode);
            response = MobileReturnCodeSupport
                    .buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null, KpiInfoResponse.class);
            response.setSurveyColligateInfo("5");
            response.setSurveyCompleteInfo("10");
            response.setSurveyContactInfo("15");
        } catch (Exception e) {
            LogUtils.error("获取绩效看板信息出错,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    KpiInfoResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    @SuppressWarnings("unused")
    @RequestMapping("getDamageList.action")
    @ResponseBody
    public String getDamageList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String logUUid = UUIDUtils.getUUIDStr();
        String logTitle = "getDamageList.action入参=" + logUUid;
        DamageListRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, DamageListRequest.class);
        DamageListResponse response = null;
        String result = "";
        try {
            // TODO 根据案件号获取历史出险信息【调用理赔】
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                    DamageListResponse.class);
        } catch (Exception e) {
            LogUtils.error("获取历史出险信息,错误信息：", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    DamageListResponse.class);
        } finally {
            result = decorateResult(response);
        }
        return result;
    }

    /**
     * 
     * @Description:MP11自助：获取报案信息
     * @return
     * @ReturnType:boolean
     * @exception @Since:2016-9-28
     * 下午02:53:14
     * @Author:liuzhipeng
     */
    @RequestMapping("getRegistInfo.action")
    @ResponseBody
    public String getRegistInfo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP11入参=" + loguuid;
        CaseInfoSelfRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseInfoSelfRequest.class);
        request.setLogUuid(loguuid);
        CaseInfoSelfResponse response = new CaseInfoSelfResponse();
        String result = "";
        try {
            response = mobileBussService.getRegistInfo(request);
        } catch (Exception e) {
            LogUtils.error("MP11->logUuid:" + loguuid + "自助：获取报案信息，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    CaseInfoSelfResponse.class);
        } finally {
            result = JsonUtils.toJsonFormatDate(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP11->logUuid:" + loguuid + "返回结果【" + result + "】");
            } else {
                LogUtils.error("MP11->logUuid:" + loguuid + "返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * 
     * @Description:获取案件状态接口 MP07
     * @return
     * @ReturnType:
     * @exception
     * @Since:2016-10-9 上午18:05:29
     * @Author:zhoupan
     */
    @RequestMapping("getCaseStatus.action")
    @ResponseBody
    public String getCaseStatus(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP07入参=" + loguuid;
        CaseStatusRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseStatusRequest.class);
        CaseStatusResponse response = new CaseStatusResponse();
        String result = "";
        try {
            String caseNo = request.getCaseNo();
            String lossIds = request.getLossIds();
            String userCode = request.getUserCode();
            // 解析lossIds数组
            String aaaString = lossIds.substring(0, lossIds.length());
            if (StringUtils.isNotBlank(aaaString) && aaaString.contains(",")) {
                String items[] = StringUtils.split(aaaString, ",");
                List<String> uuids = new ArrayList<String>();
                for (int i = 0; i < items.length; i++) {
                    String uuid = items[i].substring(0, items[i].length());
                    uuids.add(uuid);
                }
                List<PrpmLossItem> prpmLossItems = lossItemService.getLossItemByUuids(uuids);
                List<LossItemDto> lossItemList = new ArrayList<LossItemDto>();
                for (PrpmLossItem prpmLossItem : prpmLossItems) {
                    if (userCode.equals(prpmLossItem.getSurveyorCode())) {
                        LossItemDto lossItemDto = new LossItemDto();
                        lossItemDto.setLossId(prpmLossItem.getLossId().toString());
                        lossItemDto.setLossStatus(prpmLossItem.getLossStatus().toString());
                        lossItemList.add(lossItemDto);
                    }
                }
                response.setCaseNo(caseNo);
                response.setCaseUuid(prpmLossItems.get(0).getCaseNo());
                response.setLossItemList(lossItemList);
                response.setResultCode(MobileReturnCodeSupport.CODE_SUCCESS);
                response.setSystemCode(MobileReturnCodeSupport.SYS_CODE);
            } else {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, null,
                        CaseStatusResponse.class);
            }
        } catch (Exception e) {
            e.printStackTrace();
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    CaseStatusResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("返回结果【" + result + "】");
            } else {
                LogUtils.error("返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP21
     * 
     * @Desc: 获取案件排队情况接口
     * @Title: 获取案件排队情况接口
     * @Author: liuzhipeng 2016年10月19日 上午10:50:00
     * @param data
     * @return
     */
    @RequestMapping("checkVideoCase.action")
    @ResponseBody
    public String checkVideoCase(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP21入参=" + loguuid;
        CaseInfoSelfRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseInfoSelfRequest.class);
        CaseInfoSelfResponse response = null;
        String phoneNo = request.getPhoneNo();
        String caseNo = request.getCaseNo();
        String caseUuid = request.getCaseUuid();
        String result = "";
        try {
            if (StringUtils.isBlank(phoneNo)) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.KEY_MOBILE_00018, CaseInfoSelfResponse.class);
            } else if (StringUtils.isBlank(caseNo)) {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                        MobileReturnCodeSupport.KEY_MOBILE_00027, CaseInfoSelfResponse.class);
            } else if (StringUtils.isBlank(caseUuid)) {
                response = MobileReturnCodeSupport.buildReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, "案件唯一标识为空",
                        CaseInfoSelfResponse.class);
            } else {
                // 根据uuid查询预占排队日志表
                // List<PrpmCampOnQueueLog> queueLogs = campOnQueueLogService.selectByCaseNoOrdByCreatTime(caseNo,
                // phoneNo);
                List<PrpmCamponqueueLog> queueLogs = prpmCamponqueueLogService.selectByCaseNoAndPhoneNo(caseNo, phoneNo, caseUuid);
                if (queueLogs != null && queueLogs.size() > 0) {
                    MobileQueueDto queueDto = new MobileQueueDto();
                    queueDto.setCaseNo(caseNo);
                    if ("1".equals(queueLogs.get(0).getResultFlag())) {
                        queueDto.setResultFlag("1");
                    } else {
                        queueDto.setResultFlag("0");
                    }
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            CaseInfoSelfResponse.class);
                    response.setQueueDto(queueDto);
                } else {
                    MobileQueueDto queueDto = new MobileQueueDto();
                    queueDto.setCaseNo(caseNo);
                    queueDto.setResultFlag("0");
                    response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                            CaseInfoSelfResponse.class);
                    response.setQueueDto(queueDto);
                }
            }
        } catch (Exception e) {
            LogUtils.error("获取案件下定损列表失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    CaseInfoSelfResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP21返回结果【" + result + "】");
            } else {
                LogUtils.error("MP21返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP10
     * @Description:自助：转人工查勘
     * @author Liuzhipeng 2016年10月17日 上午11:25:30
     * @return
     * @throws Exception
     */
    @RequestMapping("turnPersonSurvey.action")
    @ResponseBody
    public String turnPersonSurvey(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP10入参=" + loguuid;
        TurnPersonSurveyRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, TurnPersonSurveyRequest.class);
        request.setLogUuid(loguuid);
        BaseResponse response = new BaseResponse();
        String result = "";
        try {
            response = mobileBussService.requestTurnPeronSurvey(request);
        } catch (Exception e) {
            LogUtils.error("MP10->logUuid:转人工查勘失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    CaseInfoSelfResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP10->logUuid:返回结果【" + result + "】");
            } else {
                LogUtils.error("MP10->logUuid:返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP52
     * 1.客户端点击案件中心，初始化案件列表时调用；
     * 
     * @Title：获取照片补传案件列表
     * @author zhangyw 2016年10月14日 上午11:06:07
     * @param data
     * @return String
     */
    @RequestMapping("/getBCCaseList.action")
    @ResponseBody
    public String getBCCaseList(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        /*
         * request中有caseType 和 phoneNo两个参数； 当前代码只用的了phoneNo这个参数
         */
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP52入参=" + loguuid;
        CaseInfoDtoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseInfoDtoRequest.class);
        CaseInfoDtoResponse response = new CaseInfoDtoResponse();
        String phoneNo = request.getPhoneNo();
        String result = "";
        try {
            List<CaseInfoCDto> caseInfoList = lossItemCypService.selectByPhoneNo(phoneNo);
            if (caseInfoList != null && caseInfoList.size() > 0) {
                for (CaseInfoCDto caseInfoCDto : caseInfoList) {
                    caseInfoCDto.setCaseStatusDesc(Constant.DEALSTATUSMAP.get(caseInfoCDto.getCaseStatus()));
                }
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        CaseInfoDtoResponse.class);
                response.setCaseInfoList(caseInfoList);
            } else {
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        CaseInfoDtoResponse.class);
            }
        } catch (Exception e) {
            LogUtils.error("MP52--获取照片补传案件列表--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    CaseInfoDtoResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP52--获取照片补传案件列表--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP52--获取照片补传案件列表--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP49
     * 1.查询案件对应的保单信息。
     * 2.需要调用理赔接口。
     * 
     * @Title：查询任务详细信息（照片补传,人伤预审核，单证补传）
     * @author zhangyw 2016年10月14日 下午2:53:10
     * @param data
     * @return json
     */
    @RequestMapping("/getPolicyItem31.action")
    @ResponseBody
    public String getPolicyItem31(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        /*
         * request中有mobile 、caseNo、lossUuid三个参数； 当前代码只用的了lossUuid（对应旧代码中的lossItemUuid）这个参数
         */
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP49入参=" + loguuid;
        GetPolicyItem31Request request = JsonUtils.fromJsonWithBase64(logTitle, data, GetPolicyItem31Request.class);
        GetPolicyItem31Response response = new GetPolicyItem31Response();
        String result = "";
        try {
            String lossUuid = request.getLossUuid();
            if (lossUuid != null && !"".equals(lossUuid)) {
                // 该查询输入三个参数，比之前版本多了校验功能。
                PrpmCaseInfoCyp caseInfoCyp = caseInfoCypService.selectCaseInfoByRequest(request);
                PrpmLossItemCyp lossItemCyp = lossItemCypService.selectByRequest(request);
                CaseInfoDto caseInfoDto = new CaseInfoDto();
                List<PolicyItemDto> policyItemDtoList = new ArrayList<PolicyItemDto>();
                List<PrpmLossitemcheck78Dto> auditInfoList = new ArrayList<PrpmLossitemcheck78Dto>();
                List<ReassignMent> reassList = new ArrayList<ReassignMent>();
                if (null != caseInfoCyp) {
                    caseInfoDto.setCaseNo(caseInfoCyp.getCaseNo());// 报案号
                    caseInfoDto.setLicenceNo(caseInfoCyp.getLicenseNo());// 车牌号
                    caseInfoDto.setUserName(caseInfoCyp.getUserName());// 被保险人
                    caseInfoDto.setDamageDate(DateUtil.date24ToStr(caseInfoCyp.getDamageDate()));// 出险时间
                    caseInfoDto.setDamagePlace(caseInfoCyp.getDamagePlace());// 出险地点
                    caseInfoDto.setSurveyPalce(caseInfoCyp.getSurveyPlace());// 查勘地点/收单地址
                    caseInfoDto.setSurveyDate(DateUtil.date24ToStr(caseInfoCyp.getSurveyDate()));// 查勘时间/收单时间
                    caseInfoDto.setDelegateReamrk(caseInfoCyp.getDelegateReamrk());// 案件说明
                    caseInfoDto.setUuid(caseInfoCyp.getCaseInfoCypId());// 案件唯一标识
                    caseInfoDto.setFillTheContent("补传内容");// 补传内容
                    if (null != lossItemCyp) {
                        caseInfoDto.setContact(lossItemCyp.getContact());// 联系人
                        caseInfoDto.setContactPhone(lossItemCyp.getContactPhone());// 联系电话
                        caseInfoDto.setAuditNumber(String.valueOf(lossItemCyp.getVerifyTime()));// 审核次数
                        caseInfoDto.setInjuryContent(lossItemCyp.getInjuryContent());// 人伤审核资料
                        caseInfoDto.setOtherRemark(lossItemCyp.getRemark());// 备注
                        caseInfoDto.setFillTheContent(lossItemCyp.getInformation());
                        caseInfoDto.setOtherRemark(lossItemCyp.getRemark());// 备注
                    }
                    PolicyItemDto policyItemDto = new PolicyItemDto();
                    policyItemDto.setPolicyNo(caseInfoCyp.getPolicyCode());
                    policyItemDto.setRiskCode(caseInfoCyp.getInsuranceType());
                    policyItemDto.setRiskName(caseInfoCyp.getInsuranceClass());
                    policyItemDto.setTbName(caseInfoCyp.getPolicyHolder());
                    policyItemDto.setInsurantName(caseInfoCyp.getUserName());
                    policyItemDto.setRiskClass(caseInfoCyp.getInsuranceClass());
                    if(StringUtils.isNotBlank(policyItemDto.getPolicyNo())){
                        policyItemDtoList.add(policyItemDto);
                    }
                    List<PrpmLossItemCheckCyp> itemcheckList = lossItemCheckCypService.selectByRequest(request);
                    OssManage ossManage = new OssManage();
                    if (CollectionUtils.isNotEmpty(itemcheckList)) {
                        for (PrpmLossItemCheckCyp prpmLossitemcheck78 : itemcheckList) {
                            PrpmLossitemcheck78Dto lossitemcheck78Dto = new PrpmLossitemcheck78Dto();
                            lossitemcheck78Dto.setAuditOpinion(prpmLossitemcheck78.getAuditOpinion());
                            lossitemcheck78Dto.setReviewer(prpmLossitemcheck78.getAuditorName());
                            lossitemcheck78Dto.setAuditTime(DateUtil.date24ToStr(prpmLossitemcheck78.getCreateTime()));
                            List<ReviewImage> imageList = new ArrayList<ReviewImage>();
                            List<PrpmUploadLossItemImg> itemImgList = prpmUploadLossItemimgService.selectByCheckUuid(prpmLossitemcheck78
                                    .getLossItemCheckCypId());
                            if (CollectionUtils.isNotEmpty(itemImgList)) {
                                for (PrpmUploadLossItemImg prpmUploadLossItemimg : itemImgList) {
                                    ReviewImage image = new ReviewImage();
                                    image.setLocalPath(prpmUploadLossItemimg.getImgPath());
                                    image.setOssUrl(ossManage.getImgURl(prpmUploadLossItemimg.getOssKey()));
                                    imageList.add(image);
                                }
                            }
                            lossitemcheck78Dto.setReviewImagesList(imageList);
                            auditInfoList.add(lossitemcheck78Dto);
                        }
                    }
                    //TODO
                    List<PrpmDictDetail> dictDetailList = prpmDictDetailService.getDictDetailListByDtCode(DT_CODE);
                    Map<String,String> dictDetailMap = new HashMap<String, String>();
                    if(dictDetailList != null && dictDetailList.size() > 0){
                        for (PrpmDictDetail prpmDictDetail : dictDetailList) {
                            dictDetailMap.put(prpmDictDetail.getDictCode(), prpmDictDetail.getDictName());
                        }
                    }
                    // 审核历史
                    List<PrpmLossItemCypHistory> lossItemCypHisList = lossItemCypHistoryService.getLossItemCypHistoryByRequest(request);
                    if(lossItemCypHisList != null){
                        for(PrpmLossItemCypHistory lossItemCypHis : lossItemCypHisList){
                            ReassignMent reassignMent = new ReassignMent();
                            if(lossItemCypHis.getCreateTime() != null){
                                reassignMent.setApplicantTime(DateUtil.dateToStrs(lossItemCypHis.getCreateTime()));
                            }
                            reassignMent.setReassignAddr(lossItemCypHis.getCollectAddress());
                            reassignMent.setContact(lossItemCypHis.getContact());
                            reassignMent.setContactPhone(lossItemCypHis.getContactPhone());
                            if(StringUtils.endsWith("6", lossItemCypHis.getHandleStatus())){//0首次调度1申请改派2改派
                                reassignMent.setOperateType("1");
                                reassignMent.setOperateTypeDesc("申请改派");
                                reassignMent.setReassignmentCause(dictDetailMap.get(lossItemCypHis.getReassignmentCause()));
                                reassignMent.setApplicant(lossItemCypHis.getCreateBy());
                                reassignMent.setReassignMsg(lossItemCypHis.getReassignmentReason());
                            }else if(StringUtils.endsWith("7", lossItemCypHis.getHandleStatus())){
                                reassignMent.setOperateType("2");
                                reassignMent.setOperateTypeDesc("改派");
                                reassignMent.setApplicant(lossItemCypHis.getSurveyorName());
                                reassignMent.setReassignMsg(lossItemCypHis.getReassignMsg());
                            }else if(StringUtils.endsWith("0", lossItemCypHis.getHandleStatus())){
                                reassignMent.setOperateType("0");
                                reassignMent.setOperateTypeDesc("首次调度");
                                reassignMent.setApplicant(lossItemCypHis.getSurveyorName());
                                reassignMent.setReassignMsg(lossItemCypHis.getRemark());
                            }else if(StringUtils.endsWith("8", lossItemCypHis.getHandleStatus())){
                                reassignMent.setOperateType("2");
                                reassignMent.setOperateTypeDesc("主动认领");
                                reassignMent.setApplicant(lossItemCypHis.getSurveyorName());
                                reassignMent.setReassignMsg(lossItemCypHis.getReassignMsg());
                            }
                            reassList.add(reassignMent); 
                        }
                    }
                    caseInfoDto.setAuditInfoList(auditInfoList);
                    caseInfoDto.setReassList(reassList);
                }
                response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, null,
                        GetPolicyItem31Response.class);
                response.setCaseInfo(caseInfoDto);
                response.setPolicyItemDtoList(policyItemDtoList);
            }
        } catch (Exception e) {
            LogUtils.error("MP49--查询任务详细信息（照片补传,人伤预审核，单证补传）--失败，", e);
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, null,
                    GetPolicyItem31Response.class);
        } finally {
            result = JsonUtils.toJson(response);
            // 日志记录响应json
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP49--查询任务详细信息（照片补传,人伤预审核，单证补传）--返回结果【" + result + "】");
            } else {
                LogUtils.error("MP49--查询任务详细信息（照片补传,人伤预审核，单证补传）--返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP65获取待认领任务
     * @return
     */
    @RequestMapping("getCaseInfoByCaseNo.action")
    @ResponseBody
    public String getCaseInfoByCaseNo(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP65入参=" + loguuid;
        CaseInfoDtoRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, CaseInfoDtoRequest.class);
        String caseNo = request.getCaseNo();
        String userCode = request.getUserId();
        CaseInfoDtoPnResponse response = new CaseInfoDtoPnResponse();
        String result = "";
        try {
            PrpmSurveyUser user = prpmSurveyUserService.findUserByUserCode(userCode);
            Map<String, Object> map = new HashMap<String, Object>();
            if (null == user) {
                throw new Exception("查勘平台：查勘员信息不存在!");
            } else {
                map.put("compyUuid", user.getOrgId());
            }
            map.put("caseNo", caseNo);
            List<PrpmLossItemCyp> prpmLossItemList = lossItemCypService.selectLossByCaseNo(caseNo);
            List<CaseInfoDtoPn> caseInfoList = new ArrayList<CaseInfoDtoPn>();
            String extMessage = "";
            if (CollectionUtils.isNotEmpty(prpmLossItemList)) {
                for (PrpmLossItemCyp prpmLoss : prpmLossItemList) {
                    if ("1".equals(prpmLoss.getHandleStatus()) && "2".equals(prpmLoss.getClaimStatus())) {
                        continue;
                    }
                    CaseInfoDtoPn caseInfoDto = new CaseInfoDtoPn();
                    caseInfoDto.setCaseNo(prpmLoss.getCaseNo());
                    caseInfoDto.setCaseUuid(prpmLoss.getCaseInfoCypId());
                    caseInfoDto.setHandleStatus(prpmLoss.getHandleStatus());
                    caseInfoDto.setLossUuid(prpmLoss.getLossItemCypId());
                    caseInfoDto.setVerifyTime(prpmLoss.getVerifyTime() == null ? "0" : String.valueOf(prpmLoss.getVerifyTime()));
                    caseInfoList.add(caseInfoDto);
                }
                if (CollectionUtils.isNotEmpty(caseInfoList)) {
                    extMessage = "";
                } else {
                    extMessage = MobileReturnCodeSupport.KEY_MOBILE_00016;
                }
            } else {
                extMessage = MobileReturnCodeSupport.KEY_MOBILE_00016;
            }
            response = MobileReturnCodeSupport.buildReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE, extMessage,
                    CaseInfoDtoPnResponse.class);
            response.setCaseInfoList(caseInfoList);
        } catch (Exception e) {
            LogUtils.error("MP65--获取待认领任务--失败，案件号：【" + caseNo + "】", e);
            response = MobileReturnCodeSupport.buildReponse2(MobileReturnCodeSupport.CODE_INTERNAL_ERROR, MobileReturnCodeSupport.SYS_CODE, e.getMessage(),
                    CaseInfoDtoPnResponse.class);
        } finally {
            result = JsonUtils.toJson(response);
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP65--获取待认领任务-案件号：【" + caseNo + "】-返回结果【" + result + "】");
            } else {
                LogUtils.error("MP65--获取待认领任务-案件号：【" + caseNo + "】-返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }

    /**
     * MP66确认认领案件
     * @return
     */
    @RequestMapping("affirmClaimCase.action")
    @ResponseBody
    public String affirmClaimCase(@ModelAttribute(AdapterConstants.MOBILE_REQUEST_PARAM_NAME) String data) {
        String loguuid = UUIDUtils.getUUIDStr();
        String logTitle = "MP66入参=" + loguuid;
        AffirmClaimCaseRequest request = JsonUtils.fromJsonWithBase64(logTitle, data, AffirmClaimCaseRequest.class);
        String caseNo = request.getCaseNo();
        String userCode = request.getUserId();
        String lossItemUuid = request.getLossItemUuid();
        BaseResponse response = new BaseResponse();
        String result = "";
        try {
            PrpmSurveyUser user = prpmSurveyUserService.findUserByUserCode(userCode);
            PrpmLossItemCyp prpmLossItem = lossItemCypService.selectByPrimaryKey(lossItemUuid);
            String extMessage = "";
            if (null == user) {
                extMessage = "查勘平台：查勘员信息不存在";
                response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, extMessage);
            } else {
                if (null == prpmLossItem) {
                    extMessage = "查勘平台：案件信息不存在";
                    response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, extMessage);
                } else {
                    if ("2".equals(prpmLossItem.getHandleStatus())) {
                        extMessage = "查勘平台：案件已审核完成";
                        response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, extMessage);
                    } else if ("5".equals(prpmLossItem.getHandleStatus())) {
                        extMessage = "查勘平台：案件已取消";
                        response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, extMessage);
                    } else if ("1".equals(prpmLossItem.getHandleStatus()) && "2".equals(prpmLossItem.getClaimStatus())) {
                        extMessage = "查勘平台：案件已在审核认领中";
                        response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE, extMessage);
                    } else {
                        prpmLossItem.setSurveyorCode(user.getSurveyUserCode());
                        prpmLossItem.setSurveyorName(user.getSurveyName());
                        prpmLossItem.setSurveyUnitCode(user.getOrgCode());
                        prpmLossItem.setMobile(user.getMobile());
                        prpmLossItem.setModifyTime(new Date());
                        prpmLossItem.setModifyBy(user.getSurveyUserCode());
                        prpmLossItem.setHandleStatus("8");// 主动认领
                        // 调用调度认领收单接口
                        ResultInfo resultInfo = ClaimInterface.claimCollectTask(user.getSurveyUserCode(), caseNo, "0");
                        if (null == resultInfo) {
                            extMessage = "查勘平台：调用DP14接口失败，无返回信息";
                            response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                                    extMessage);
                        } else {
                            if ("000".equals(resultInfo.getResultCode())) {
                                int ret = lossItemCypService.updateByPrimaryKeySelective(prpmLossItem);
                                if (ret > 0) {
                                    response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_SUCCESS, MobileReturnCodeSupport.SYS_CODE,
                                            "");
                                } else {
                                    extMessage = "同步调度成功，更新查勘数据库失败!";
                                    response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                                            extMessage);
                                }
                            } else {
                                extMessage = "查勘平台：调用DP14接口失败:" + resultInfo.getResultMsg();
                                response = MobileReturnCodeSupport.buildBaseReponse2(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                                        extMessage);
                            }
                        }
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            LogUtils.error("error=" + e.toString());// 记录日志
            response = MobileReturnCodeSupport.buildBaseReponse(MobileReturnCodeSupport.CODE_FAIL, MobileReturnCodeSupport.SYS_CODE,
                    MobileReturnCodeSupport.KEY_MOBILE_00016);
        } finally {
            result = JsonUtils.toJson(response);
            if (MobileReturnCodeSupport.CODE_SUCCESS.equals(response.getResultCode())) {
                LogUtils.info("MP66--确认认领案件-案件号：【" + caseNo + "】-返回结果【" + result + "】");
            } else {
                LogUtils.error("MP66--确认认领案件-案件号：【" + caseNo + "】-返回结果【" + result + "】");
            }
            result = EncryptUtil.encodeBase64(result);
        }
        return result;
    }
}
